package idv.arthur.prac;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.tmatesoft.svn.core.ISVNDirEntryHandler;
import org.tmatesoft.svn.core.SVNCancelException;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNEvent;
import org.tmatesoft.svn.core.wc.SVNEventAction;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;

public class SvnTest {
	static {  
        DAVRepositoryFactory.setup();  
    }  
  
    private SVNClientManager manager;  
    private SVNURL repositoryBaseUrl;  
  
    public SvnTest() {  
        DefaultSVNOptions options = new DefaultSVNOptions();  
        manager = SVNClientManager.newInstance(options);  
        // manager = SVNClientManager.newInstance(options,  
        // "username","passwrod"); //如果需要用戶名密碼  
        try {  
            repositoryBaseUrl = SVNURL.parseURIEncoded("http://10.144.136.56/svntest/arthur"); // 傳入svn地址  
        } catch (SVNException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
  
    }
    public class DirEntryHandler implements ISVNDirEntryHandler {  
    	  
        @Override  
        public void handleDirEntry(SVNDirEntry dirEntry) throws SVNException {  
      
            System.out.println(dirEntry.getRelativePath() + "/" + dirEntry.getName());  
        }  
      
    }  
  
    public void test() throws SVNException {  
        SVNLogClient logClient = manager.getLogClient();  
  
        // svn list  
        DirEntryHandler handler = new DirEntryHandler(); // 在svn  
                                                            // co時對每個文件目錄的處理，實現ISVNDirEntryHandler接口  
        logClient.doList(repositoryBaseUrl, SVNRevision.HEAD, SVNRevision.HEAD,false, true, handler); // 列出當前svn地址的目錄，對每個文件進行處理  
  
        // svn co  
        UpdateEventHandler svnEventHandler = new UpdateEventHandler(); // svn  co時對每個文件的處理  
        SVNUpdateClient client = manager.getUpdateClient();  
        client.setIgnoreExternals(true);  
        client.setEventHandler(svnEventHandler);  
        File to = new File("D:\\temp"); // co出來的文件存放目錄  
        client.doCheckout(repositoryBaseUrl, to, SVNRevision.HEAD,  
                SVNRevision.HEAD, SVNDepth.INFINITY, false);  
  
        // svn update  
        client.setIgnoreExternals(true);  
        client.setEventHandler(svnEventHandler);   
  
        client.doUpdate(to, SVNRevision.HEAD, SVNDepth.INFINITY,true, false);  
  
    }  
	public static void main(String[] args) throws SVNException {
		SvnTest svntest = new SvnTest();  
        svntest.test();  

	}

	public class UpdateEventHandler implements ISVNEventHandler {  
		  
	    public void handleEvent(SVNEvent event, double progress) {  
	        SVNEventAction action = event.getAction();  
	        SVNNodeKind nodeKind = event.getNodeKind();  
	  
	        if (SVNNodeKind.DIR.equals(nodeKind)) {  
	            // folder  
	                System.out.println(event.getFile().getName());  
	  
	        } else {  
	            // treat as file for all other type  
	            if (action == SVNEventAction.UPDATE_DELETE) {  
	                try {  
	                    System.out.println(event.getFile().getName() + "\t" + FileUtils.readFileToString(event.getFile()));  
	                } catch (IOException e) {  
	  
	                }  
	            } else if (action == SVNEventAction.UPDATE_ADD || action == SVNEventAction.UPDATE_UPDATE) {  
	                try {  
	                    System.out.println(event.getFile().getName() + "\t" + FileUtils.readFileToString(event.getFile()));  
	                } catch (IOException e) {  
	  
	                }  
	            }  
	        }  
	    }  
	  
	    public void checkCancelled() throws SVNCancelException {  
	    }  
	  
	}  
	
}
